package com.net.xpay.openapi.controller.openapi.common;

import com.google.common.base.Strings;
import com.net.xpay.openapi.aop.OpenApi;
import com.net.xpay.openapi.dto.OpenApiChargeRecordChargeDto;
import com.net.xpay.openapi.dto.OpenApiChargeRecordQueryDto;
import com.net.xpay.openapi.model.OpenApiChargeRecordChargeModel;
import com.net.xpay.openapi.model.OpenApiChargeRecordResultQueryModel;
import com.net.xpay.openapi.protocol.OpenApiProtocol;
import com.net.xpay.openapi.protocol.OpenApiResult;
import com.net.xpay.openapi.service.OpenApiChargeRecordService;
import com.net.common.exception.BusinessException;
import com.net.common.util.MoneyUtil;
import com.net.common.util.SerializationUtil;
import com.net.xpay.common.annotation.ApiMonitoring;
import com.net.xpay.common.domain.User;
import com.net.xpay.common.domain.openapi.OpenApp;
import com.net.xpay.common.manager.UserManager;
import com.net.xpay.common.manager.openapi.OpenAppManager;
import com.net.xpay.core.constant.PoseidonErrorCode;
import com.net.xpay.core.domain.ChargeRecord;
import com.net.xpay.core.enums.PayStatus;
import com.net.xpay.core.manager.ChargeRecordManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@RequestMapping("/apiv1/openapi/charge-record")
@Api(tags = "第三方开放调用/充值服务费", description = "第三方开放调用 充值服务费")
public class OpenApiChargeRecordController {

    @Autowired
    private OpenApiChargeRecordService openApiChargeRecordService;

    @Autowired
    private ChargeRecordManager chargeRecordManager;

    @Autowired
    private UserManager userManager;

    @Autowired
    private OpenAppManager openAppManager;

    /**
     * 发起充值
     */
    @OpenApi
    @ApiMonitoring(value = "充值服务费/充值服务费")
    @ApiOperation("发起充值")
    @PostMapping("/charge")
    public OpenApiResult<OpenApiChargeRecordChargeDto> charge(@RequestBody OpenApiProtocol openApiProtocol) {
        OpenApp openApp = openAppManager.getById(Long.valueOf(openApiProtocol.getAppId()));
        OpenApiChargeRecordChargeModel model = SerializationUtil.str2Object(openApiProtocol.getBizContent(), OpenApiChargeRecordChargeModel.class);

        User user = userManager.getById(openApp.getUserId());
        String url = openApiChargeRecordService.charge(user, model);

        OpenApiChargeRecordChargeDto dto = new OpenApiChargeRecordChargeDto();
        dto.setPayUrl(url);
        return new OpenApiResult<>(dto);
    }

    /**
     * 查询充值结果
     */
    @OpenApi
    @ApiMonitoring(value = "充值服务费/查询服务费充值结果")
    @ApiOperation("查询充值结果")
    @PostMapping("/query-charge-record-result")
    public OpenApiResult<OpenApiChargeRecordQueryDto> queryChargeRecordResult(@RequestBody OpenApiProtocol openApiProtocol) {
        OpenApp openApp = openAppManager.getById(Long.valueOf(openApiProtocol.getAppId()));
        OpenApiChargeRecordResultQueryModel queryModel = SerializationUtil.str2Object(openApiProtocol.getBizContent(), OpenApiChargeRecordResultQueryModel.class);

        if (Strings.isNullOrEmpty(queryModel.getImportBizNo())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "请输入外部订单号");
        }
        User user = userManager.getById(openApp.getUserId());
        ChargeRecord chargeRecord = chargeRecordManager.findByImportBizNo(user.getId(),queryModel.getImportBizNo());
        if (chargeRecord == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "充值订单不存在");
        }
        OpenApiChargeRecordQueryDto dto = OpenApiChargeRecordQueryDto.getInstance();
        dto.setAmount(MoneyUtil.convertCentToString(chargeRecord.getAmount(), 2));
        dto.setChargeRecordId(chargeRecord.getId().toString());
        dto.setTradeNo(chargeRecord.getDetailId());
        dto.setImportBizNo(chargeRecord.getImportBizNo());
        dto.setPayStatus(chargeRecord.getPayStatus());
        if (chargeRecord.getPayStatus() == PayStatus.PAYED) {
            dto.setPayTime(chargeRecord.getEndTime());
        }
        dto.setCreateTime(chargeRecord.getCreateTime());
        return new OpenApiResult<>(dto);
    }
}
